﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dodone
{
    /// <summary>
    /// Interaction logic for CalendarWeekPanel.xaml
    /// </summary>
    public partial class CalendarWeekPanel : UserControl
    {
        private Label[] lblDay = new Label[7];
		private Label[] lblWeekday = new Label[7];
		private string[] strWeekday = { "일", "월", "화", "수", "목", "금", "토" };
		private CalendarWeekItem[] weekItems = new CalendarWeekItem[7];
		private CalendarWindow ancestorWindow;
		private CalendarWeekItem selectedItem = new CalendarWeekItem();

        public CalendarWeekPanel()
        {
            InitializeComponent();

            DayGrid.Children.Clear();
			WeekdayGrid.Children.Clear();
			WeekItemGrid.Children.Clear();

            for (int i = 0; i < lblDay.Length; i++)
            {
                lblDay[i] = new Label();
                lblDay[i].HorizontalAlignment = HorizontalAlignment.Center;
                lblDay[i].FontSize = 11;
                DayGrid.Children.Add(lblDay[i]);

				lblWeekday[i] = new Label();
				lblWeekday[i].HorizontalAlignment = HorizontalAlignment.Center;
				lblWeekday[i].FontSize = 11;
				lblWeekday[i].Content = strWeekday[i] + "요일";
				WeekdayGrid.Children.Add(lblWeekday[i]);

				weekItems[i] = new CalendarWeekItem();
				weekItems[i].MouseLeftButtonDown += WeekItem_MouseLeftButtonDown;
				WeekItemGrid.Children.Add(weekItems[i]);
            } // end for
        }

		public CalendarWindow AncestorWindow
		{
			get { return ancestorWindow; }
			set { ancestorWindow = value; }
		}

        public void prtWeekCal(DateTime iWeek)
        {
            DateTime firstDay = iWeek;
            int fIndex = -(int)firstDay.DayOfWeek;

			selectedItem = new CalendarWeekItem();

            firstDay = firstDay.AddDays(fIndex);

            for (int i = 0; i < lblDay.Length; i++)
            {
				if (firstDay == AncestorWindow.selectDate)
					selectedItem = weekItems[i];

                lblDay[i].Content = firstDay.Month.ToString() + "/" + firstDay.Day.ToString();

				weekItems[i].thisDate = firstDay;
				paintItem(weekItems[i], lblDay[i], lblWeekday[i]);
                firstDay = firstDay.AddDays(1);
            } // end for
        }

		private void paintItem(CalendarWeekItem weekItem, Label dayItem, Label weekdayItem)
		{
			// back color
			if (weekItem.thisDate == selectedItem.thisDate) // selected day
				modifyPanelColor(weekItem, Color.FromRgb(255, 200, 200));
			else if (weekItem.thisDate == DateTime.Today) // today
				modifyPanelColor(weekItem, Color.FromRgb(255, 255, 200));
			else
				modifyPanelColor(weekItem, Colors.White);

			// style
			if (weekItem.thisDate == selectedItem.thisDate) // selected day
			{
				dayItem.FontWeight = FontWeights.Bold;
				weekdayItem.FontWeight = FontWeights.Bold;
			}
			else
			{
				dayItem.FontWeight = FontWeights.Normal;
				weekdayItem.FontWeight = FontWeights.Normal;
			}
		}

		private void modifyPanelColor(CalendarWeekItem weekItem, Color color)
		{
			weekItem.Background = new SolidColorBrush(color);
			weekItem.listBoxEvent.Background = new SolidColorBrush(color);
		}

		private void WeekItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			CalendarWeekItem thisItem = e.Source as CalendarWeekItem;
			CalendarWeekItem selItem = selectedItem;
			selectedItem = thisItem;
			paintItem(selItem, lblDay[(int)selItem.thisDate.DayOfWeek], lblWeekday[(int)selItem.thisDate.DayOfWeek]); // recovery original color
			paintItem(thisItem, lblDay[(int)thisItem.thisDate.DayOfWeek], lblWeekday[(int)thisItem.thisDate.DayOfWeek]);
			AncestorWindow.RefreshDay(thisItem.thisDate);
		}
    }
}
